/*---------------------------------------------------------
    ev_doors.lua - Door Information
	by Lead4u
---------------------------------------------------------*/

	evorp.doors.stored = {};
	local doors = { "func_door_rotating", "func_door", "prop_door_rotating" }
	print(" === DOORS LOADED === ")
	
/*---------------------------------------------------------
    Is a Door a valid door
---------------------------------------------------------*/
	
	function evorp.doors:IsValid( door )
		if ( door:IsValid() and table.HasValue( doors, door:GetClass() ) ) then
			return true;
		else
			return false;
		end
	end

/*---------------------------------------------------------
    Is a Door already owned?
---------------------------------------------------------*/
	
	function evorp.doors:IsOwned( door )
		if ( evorp.doors.stored[door:EntIndex()] ) then
			return true;
		else
			return false;
		end
	end
	
/*---------------------------------------------------------
    Buy a Door
---------------------------------------------------------*/

	function evorp.doors:Buy( player, door, doortype )
		local trace = player:GetEyeTrace()
		local ent = trace.Entity
		
		if ( !evorp.doors:IsValid( ent ) ) then return end
		if ( evorp.doors:IsOwned( ent ) ) then print("ERROR: Already owned.") return end
		
		evorp.doors.stored[door:EntIndex()] = {};
		evorp.doors.stored[door:EntIndex()].owner = player:UniqueID();
		evorp.doors.stored[door:EntIndex()].type = doortype;
		evorp:AddMoney( player, -50 )
		evorp.doors:Info( "Buy", player:UniqueID(), door:EntIndex(), doortype )
	end
	
/*---------------------------------------------------------
    Sell a Door
---------------------------------------------------------*/

	function evorp.doors:Sell( doorID )
		evorp:AddMoney( player.GetByUniqueID( evorp.doors.stored[doorID].owner ), 50 )
		evorp.doors.stored[doorID] = nil
		evorp.doors:Info( "Sell", nil, doorID, nil )
	
	end
	
/*---------------------------------------------------------
    Find a Door by EntID
---------------------------------------------------------*/

	function evorp.doors:Find( DoorID )
	
		return ents.GetByIndex( tonumber(DoorID) )
	
	end

/*---------------------------------------------------------
    Send new Door Ownership to clients
		PID=PlayerUniqueID
---------------------------------------------------------*/

	function evorp.doors:Info( Cmd , plyID , DoorID, type )
		if ( Cmd == "Buy" ) then
			local Ply = player.GetByUniqueID( plyID )
			umsg.Start("evorp_door_add")
				umsg.String( plyID )
				umsg.Short( DoorID )
				umsg.String( Ply:Nick() )
				umsg.String( type )
			umsg.End()
		end
		
		if ( Cmd == "Sell" ) then
			umsg.Start("evorp_door_remove")
				umsg.Short ( DoorID )
			umsg.End()
		end
	end

/*---------------------------------------------------------
    Command Setup (TEMP VERY WIP)
---------------------------------------------------------*/	
	
	

concommand.Add("evorp", function( player, cmd, args )
	
	if( args[1] == "buy" ) then
		if ( !args[3] ) then return error("Bad something") end;
		evorp.doors:Buy( player , evorp.doors:Find( args[2] ), args[3] )
	end
	if ( args[1] == "sell" ) then
		evorp.doors:Sell( tonumber( args[2] ) )
	end
	if ( args[1] == "reload" ) then
		evorp.doors.NewPlayer( player )
	end
	print( cmd.. "; " ) PrintTable( args )
		
end)

/*---------------------------------------------------------
    Send current doors on spawn
---------------------------------------------------------*/	

	function evorp.doors.NewPlayer( player )
		if ( timer.IsTimer( tostring(player:UniqueID()) )) then
			timer.Destroy( player:UniqueID() )
			print("Deletion timer for " .. player:Nick() .. " has been removed.")
		end
		
		for k,v in pairs( evorp.doors.stored ) do
		
			evorp.doors:Info( "Buy" , evorp.doors.stored[k].owner , k , evorp.doors.stored[k].type )
			
		end
	end
	
	
/*---------------------------------------------------------
    Reset doors if people leave
---------------------------------------------------------*/
		
	function evorp.doors.ResetDoors( ent )
		if ( ent:IsPlayer() and ent:IsValid() ) then
			local UID = ent:UniqueID()
			local Name = ent:Nick()
			timer.Create( ent:UniqueID(), 45, 1, function()
				for k,v in pairs( evorp.doors.stored ) do
					if ( evorp.doors.stored[k].owner == UID ) then
						print(" Removing " .. Name .. " from ownership table.")
						evorp.doors:Info( "Sell", nil, k, nil )
						evorp.doors.stored[k] = nil
					end
				end
				timer.Destroy( UID )
			end)
				
		end
	end
hook.Add("EntityRemoved", "ev_entRemoved", evorp.doors.ResetDoors, evorp)

